package me.ywy.xiami;

import java.util.Iterator;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class XiaMiiiiii {

	public static void main(String[] args) {
		// 获取 music 页面json
		UserInfo user = new UserInfo("8360816"); // 虾米uid
		boolean res = user.parseBaseInfo();
		if (res) {
			user.parseJsonSecond();
		}
		// 获取 hermit json
		Collect col = new Collect("32540821");
		col.init();
		System.out.println(col.getHermitJson());
		//
		Album alb = new Album("350646");
		alb.init();
		System.out.println(alb.getHermitJson());
	}
}

class Song {
	JSONObject json;
	String soundId;

	public Song(String id) {
		this.soundId = id;
		json = null;
	}

	public void init() {
		json = Tools.httpJson(GlobalConfig.XM_SONG_URL + soundId);
	}
}

class Album {
	JSONObject json;
	String albumId;

	public Album(String id) {
		this.albumId = id;
		json = null;
	}

	public void init() {
		json = Tools.httpJson(GlobalConfig.XM_ALBUMS_URL + albumId);
		//System.out.println("album Id = " + albumId + " info = " + json);
	}

	public JSONObject getCollect() {
		JSONObject collect = new JSONObject();
		if (json != null) {
			JSONObject album = json.getJSONObject("album");

			JSONArray songstmp = album.getJSONArray("songs");
			int count = songstmp.size();
			JSONArray songs = new JSONArray();
			String author = "";
			for (int j = 0; j < count; j++) {
				JSONObject obj = songstmp.getJSONObject(j);
				JSONObject temp = new JSONObject();
				temp.put("song_id", obj.getString("song_id"));
				temp.put("song_title", obj.getString("name"));
				temp.put("song_length", obj.getString("length"));
				temp.put("song_src", obj.getString("location"));
				author = obj.getString("singers");
				temp.put("song_author", author);
				songs.add(temp);
			}
			collect.put("collect_id", album.getString("album_id"));
			collect.put("collect_title", album.getString("title"));
			collect.put("collect_author", /*author*/"★★★★★");
			collect.put("collect_type", "albums");
			collect.put("collect_cover", album.getString("album_logo"));
			collect.put("collect_count", count);
			collect.put("songs", songs);
			//System.out.println(collect);
			return collect;
		}
		return null;
	}
	
	public JSONObject getHermitJson() {
		JSONObject collect = new JSONObject();
		if (json != null) {
			JSONObject album = json.getJSONObject("album");
			JSONArray songstmp = album.getJSONArray("songs");
			int count = songstmp.size();
			JSONArray songs = new JSONArray();
			for (int j = 0; j < count; j++) {
				JSONObject obj = songstmp.getJSONObject(j);
				JSONObject temp = new JSONObject();
				temp.put("song_author", obj.getString("singers"));
				temp.put("song_cover", obj.getString("album_logo"));
				temp.put("song_id", obj.getString("song_id"));
				temp.put("song_src", obj.getString("location"));
				temp.put("song_title", obj.getString("name"));
				songs.add(temp);
			}
			collect.put("songs", songs);
			//System.out.println(collect);
			return collect;
		}
		return null;
	}
}

class Collect {
	JSONObject json;
	String collectId;

	public Collect(String id) {
		this.collectId = id;
		json = null;
	}

	public void init() {
		json = Tools.httpJson(GlobalConfig.XM_COLLECT_URL + collectId);
		//System.out.println("collect Id = " + collectId + " info = " + json);
	}

	public JSONObject getCollect() {
		JSONObject collect = new JSONObject();
		if (json != null) {
			JSONObject collectt = json.getJSONObject("collect");
			JSONArray songstmp = collectt.getJSONArray("songs");
			int count = songstmp.size();
			JSONArray songs = new JSONArray();
			for (int j = 0; j < count; j++) {
				JSONObject obj = songstmp.getJSONObject(j);
				JSONObject temp = new JSONObject();
				temp.put("song_id", obj.getString("song_id"));
				temp.put("song_title", obj.getString("name"));
				temp.put("song_length", /*obj.getString("length")*/0);
				temp.put("song_src", obj.getString("location"));
				temp.put("song_author", obj.getString("singers"));
				songs.add(temp);
			}
			collect.put("collect_id", collectt.getString("id"));
			collect.put("collect_title", collectt.getString("name"));
			collect.put("collect_author", /*collectt.getString("nick_name")*/"★★★★★");
			collect.put("collect_type", "collects");
			collect.put("collect_cover", collectt.getString("logo"));
			collect.put("collect_count", count);
			collect.put("songs", songs);
			//System.out.println(collect);
			return collect;
		}
		return null;
	}
	
	public JSONObject getHermitJson() {
		JSONObject collect = new JSONObject();
		if (json != null) {
			JSONObject collectt = json.getJSONObject("collect");
			JSONArray songstmp = collectt.getJSONArray("songs");
			int count = songstmp.size();
			JSONArray songs = new JSONArray();
			for (int j = 0; j < count; j++) {
				JSONObject obj = songstmp.getJSONObject(j);
				JSONObject temp = new JSONObject();
				temp.put("song_author", obj.getString("singers"));
				temp.put("song_cover", obj.getString("album_logo"));
				temp.put("song_id", obj.getString("song_id"));
				temp.put("song_src", obj.getString("location"));
				temp.put("song_title", obj.getString("name"));
				songs.add(temp);
			}
			collect.put("songs", songs);
			//System.out.println(collect);
			return collect;
		}
		return null;
	}
}

class UserInfo {
	String uid;
	JSONObject albumJson;
	JSONObject collectJson;
	JSONObject finalJson;

	public UserInfo(String uid) {
		this.uid = uid;
		finalJson = new JSONObject();
	}

	public boolean parseBaseInfo() {
		albumJson = Tools.httpJson(GlobalConfig.XM_U_ALBUMS_URL + uid);
		collectJson = Tools.httpJson(GlobalConfig.XM_U_COLLECT_URL + uid);
		//System.out.println("albumJson = " + albumJson);
		//System.out.println("collectJson = " + collectJson);
		if (albumJson != null || collectJson != null) {
			return true;
		} else {
			return false;
		}
	}

	public void parseJsonSecond() {
		finalJson.put("status", 200);
		JSONArray collects = new JSONArray();
		if (albumJson != null) {
			parseAlbum(collects);
		}
		if (collectJson != null) {
			parseCollect(collects);
		}
		finalJson.put("msg", collects);
		System.out.println(collects);
	}

	private void parseAlbum(JSONArray collects) {
		int albumLen = albumJson.size();
		if (albumLen <= 0) {
			return;
		}
		Iterator<?> it = albumJson.keys();
		while (it.hasNext()) {
			String key = (String) it.next();
			if (!key.equals("albums")) {
				continue;
			}
			JSONArray array = albumJson.getJSONArray(key);
			for (int i = 0; i < array.size(); i++) {
				JSONObject jsonobject = array.getJSONObject(i);
				collects.add(getCollectFromAlbum(jsonobject));
			}
		}
	}

	private void parseCollect(JSONArray collects) {
		int collectLen = collectJson.size();
		if (collectLen <= 0) {
			return;
		}
		Iterator<?> it = collectJson.keys();
		while (it.hasNext()) {
			String key = (String) it.next();
			if (!key.equals("collects")) {
				continue;
			}
			JSONArray array = collectJson.getJSONArray(key);
			for (int i = 0; i < array.size(); i++) {
				JSONObject jsonobject = array.getJSONObject(i);
				collects.add(getCollectFromCollect(jsonobject));
			}
		}
	}

	private JSONObject getCollectFromCollect(JSONObject json) {
		String albumId = json.getString("obj_id");
		Collect collect = new Collect(albumId);
		collect.init();
		return collect.getCollect();
	}

	private static JSONObject getCollectFromAlbum(JSONObject json) {
		String albumId = json.getString("obj_id");
		Album album = new Album(albumId);
		album.init();
		return album.getCollect();
	}
}